<?php

/**
 * This class manages all database queries (or other operations)
 *  concerning children.
 */
class Model_Child extends Model_Person
{

    /**
     * Lists all children, ordered alphabetically by last name.
     * 
     * @return array an array of (associative) arrays
     */
    public function listAll($where = "person_type = 'Kind'")
    {
	return parent::listAll($where);
    }

    public function listAllAktiv()
    {
	$select = $this->db->select()
		->from($this->name)
		->where('person_type = ?', 'Kind')
		->where('expire_date IS NULL')
		->order('surname ASC');
	return $this->db->fetchAll($select);
    }

    public function listAllInAktiv()
    {
	$select = $this->db->select()
		->from($this->name)
		->where ('person_type = ?', 'Kind')
		//->where ('expire_date IS NOT NULL') 
		->where ('expire_date >= ?', strtotime(date('Y-m-d H:i:s')))
		->order('surname ASC');
	return $this->db->fetchAll($select);
    }

    function create($data)
    {
	$data['person_type'] = 'Kind';
	$data['email'] = NULL;
	parent::create($data);
	return($this->db->lastInsertID());
    }

    public function update(array $child)
    {
	parent::update($child);
    }

    public function delete($id)
    {
	$where = $this->db->quoteInto('id = ?', $id);
	$this->db->delete($this->name, $where);
    }

    public function find($id)
    {
	$select = $this->db->select()
		->from($this->name)
		->where('id = ?', $id);
	$returnValue = $this->db->fetchRow($select);
	if ($returnValue['birthday'] != '')
	{
	    $returnValue['age'] = $this->calcAge($returnValue['birthday']);
	}
	return $returnValue;
    }

    public function calcAgeTimestamp($birthdate, $dateToCompare = null)
    {
	$birthdate = ($birthdate instanceof Zend_Date) ? $birthdate : new Zend_Date($birthdate);
	$dateToCompare($dateToCompare === null) ? Zend_Date::now() : $dateToCompare;
	return $dateToCompare->sub($birthdate)->getTimestamp();
    }

    public function getAgeInYearsNew($birthDate, $dateToCompare = null)
    {
		
	$birthDate = ($birthDate instanceof Zend_Date) ? $birthDate : new Zend_Date($birthDate);
	$dateToCompare = ($dateToCompare === null) ? Zend_Date::now() : $dateToCompare;
	
	$dYear = $dateToCompare->get(Zend_Date::YEAR) - $birthDate->get(Zend_Date::YEAR);
	$dMonth = $dateToCompare->get(Zend_Date::MONTH) - $birthDate->get(Zend_Date::MONTH);
	$dDay = $dateToCompare->get(Zend_Date::DAY) - $birthDate->get(Zend_Date::DAY);
	if ($dMonth < 0)
	{
	    $dYear--;
	} elseif (($dMonth == 0) && ($dDay < 0))
	    $dYear--;
	return $dYear;
    }

    /*
     * TODO: Fix age calculations...
     */

    public function calcAge($birthday, $targetDate = false)
    {
	//$age=$this->getAgeInYears($birthday);
	$retVal = 0;
	$birthday = new Zend_Date($birthday);
	$bday = new DateTime($birthday);
	if ($targetDate === false)
	    $targetDate = new Zend_Date();
	$tDate = new DateTime();
	$birthMonth = $birthday->get(Zend_Date::MONTH);
	$targetMonth = $targetDate->get(Zend_Date::MONTH);
	$targetDate->subDate($birthday);
	$age = $targetDate->get(Zend_Date::YEAR);

	$blabla = date_diff($bday, $tDate);
	//$bla=$blabla->format('%y Jahre');
	if ($age < 1)
	{
	    $retVal = 0;
	    if ($birthMonth <= $targetMonth)
	    {
		$retVal = $targetMonth - $birthMonth;
		$retVal.=' Monate';
	    } else
	    {
		$retVal = 12 - ($birthMonth - $targetMonth);
		$retVal.=' Monate';
	    }
	} else if ($age >= 1 && $age < 2)
	{
	    $retVal = 12;
	    if ($birthMonth < $targetMonth)
	    {
		$retVal+=($targetMonth - $birthMonth);
		$retVal.=' Monate';
	    } else
	    {
		$retVal+=(12 - ($birthMonth - $targetMonth));
		$retVal.=' Monate';
	    }
	} else if ($age >= 2)
	{
	    // $retVal=$age.' Jahre';
	    $retVal = $blabla->format('%y Jahre');
	}
	return $retVal;
    }

    public function getAgeInYears($birthday)
    {
	$birthday = new Zend_Date($birthday);
	$tDate = new Zend_Date();
	$tDate->subDate($birthday);
	return $tDate->toValue(Zend_Date::YEAR);
    }

    public function getAgeInMonths($birthday)
    {
	$birthday = new Zend_Date($birthday);
	$targetDate = new Zend_Date();
	$targetDate->subDate($birthday);
	return $targetDate->toValue(Zend_Date::MONTH);
    }

    public function setInaktiv($id)
    {
	$select = $this->db->select()
		->from($this->name)
		->where('id = ?', $id);
	$returnValue = $this->db->fetchRow($select);

	$date = new Zend_Date();
	$Date_now = $date->get('YYYY-MM-dd HH:mm:ss');
	$returnValue['expire_date'] = $Date_now;
	parent::update($returnValue);
    }

    public function setaktiv($id)
    {
	$select = $this->db->select()
		->from($this->name)
		->where('id = ?', $id);
	$returnValue = $this->db->fetchRow($select);

	$returnValue['expire_date'] = NULL;
	parent::update($returnValue);
    }

    public function listAllChildWithParentTelNumbers()
    {

	$select = $this->db->select()
		->from(array('plp' => 'person_link_person'), array('plp_id' => 'id'))
		->join(array('p' => 'person'), 'plp.idperson1 = p.id')
		->join(array('p1' => 'person'), 'plp.idperson2 = p1.id');

	return $this->db->fetchAll($select);
    }

}
